#include "BehaviorSeparation.h"
#include "BoidBase.h"

//! executes behavior.
Ogre::Vector3 BehaviorSeparation::execute(const BoidBase* boid)
{
	Ogre::Vector3 desiredVector(0.0f, 0.0f, 0.0f);

	const BoidList& neighbors = boid->getNeighbors();
	BoidList::const_iterator iter = neighbors.begin();

	while (iter != neighbors.end())
	{
		if (mMaxRadius <= 0.0f || boid->isInNeighborhood(*iter, mMinRadius, mMaxRadius, mCosMaxAngle))
		{
			Ogre::Vector3 offset((*iter)->getPosition() - boid->getPosition());
			float squaredDistance = offset.squaredLength();
			desiredVector += (offset / -squaredDistance);
		}

		iter++;
	}

	desiredVector.normalise();
	return desiredVector;
}
